program SHANNONs_FORMULA ;

(*
    This program uses SHANNON's formula to find out the
    capacity or bandwidth of a communications channel.
        The formula is as follows

           C  =  W  *  log  ( 1 + ( S / N ) )
                          2
    where
           C = Capacity of the channel in bits per second
           W = Bandwidth of the channel in Hertz
           S = Signal power in Watt
           N = Noise power in Watt
*)

 {$U+}

 var
   bandwidth , capacity , temp : real ;
   signal , noise , i : integer ;
   find , mes , cmes , bmes , smes , nmes : char ;

 procedure drawbox;
  var
    x , y , i :integer;
  begin
    x := 1;
    y := 1;
    for i := 1 to 80 do
     begin
       gotoxy(x,y);
       write(chr(223));
       x := x + 1;
     end;
    x := 80;
    y := 1;
    for i := 1 to 24 do
     begin
       gotoxy(x,y);
       write(chr(219));
       y := y + 1;
     end;
    x := 79;
    y := 24;
    for i := 1 to 79 do
     begin
       gotoxy(x,y);
       write(chr(220));
       x := x-1;
     end;
    x := 1;
    y := 24;
    for i := 1 to 24 do
     begin
       gotoxy(x,y);
       write(chr(219));
       y := y - 1;
     end;
  end;

 begin
   clrscr ;
   writeln ;
   writeln ;
   writeln ;
   writeln ;
   writeln('              +----------------------------------------------+') ;
   writeln('              |                                              |') ;
   writeln('              |      S H A N N O N''s      F O R M U L A     |') ;
   writeln('              |                                              |') ;
   writeln('              |                 ( ver  1.0 )                 |') ;
   writeln('              |                                              |') ;
   writeln('              +----------------------------------------------+') ;
   writeln ;
   writeln ;
   writeln ;
   writeln ;
   write('               To find ? ( [C]apacity / [B]andwidth )  ') ;
   readln(find) ;
   find := upcase(find) ;
   writeln ;
   writeln ;
   if ( find = 'C' ) then
     write('                    Unit of measure ? ( [K]bps / [B]ps )  ') ;
   if ( find = 'B' ) then
     write('                    Unit of measure ? ( [K]hertz / [H]ertz )  ') ;
   readln(mes) ;
   mes := upcase(mes) ;
   clrscr ;
   drawbox ;
   if ( find = 'C' ) then
    begin
      gotoxy(10,5) ;
      write('Enter the bandwidth   :  ') ;
      read(bandwidth) ;
      gotoxy(15,7) ;
      write('Unit of measure ? ([K]hertz / [H]ertz )  ') ;
      readln(bmes) ;
      bmes := upcase(bmes) ;
      if ( bmes = 'K' ) then
        bandwidth := bandwidth * 1000 ;
    end ;
   if ( find = 'B' ) then
    begin
      gotoxy(10,5) ;
      write('Enter the capacity    :  ') ;
      read(capacity) ;
      gotoxy(15,7) ;
      write('Unit of measure ? ([K]bps / [B]ps )  ') ;
      readln(cmes) ;
      cmes := upcase(cmes) ;
      if ( cmes = 'K' ) then
        capacity := capacity * 1000 ;
    end ;
   gotoxy(10,10) ;
   write('Enter the signal power   :  ') ;
   read(signal) ;
   gotoxy(15,12) ;
   write('Unit of measure ? ([K]watts / [W]atts )  ') ;
   readln(smes) ;
   smes := upcase(smes) ;
   if ( smes = 'K' ) then
     signal := signal * 1000 ;
   gotoxy(10,15) ;
   write('Enter the noise power    :  ') ;
   read(noise) ;
   gotoxy(15,17) ;
   write('Unit of measure ? ([K]watts / [W]atts )  ') ;
   readln(nmes) ;
   nmes := upcase(nmes) ;
   if ( nmes = 'K' ) then
     noise := noise * 1000 ;
   if ( find = 'C' ) then
    begin
      temp :=  1 + (signal/noise) ;
      capacity := bandwidth * ( ln(temp) / ln(2) ) ;
      if ( mes = 'K' ) then
        capacity := capacity / 1000 ;
      gotoxy(10,20) ;
      write('The capacity is  :  ') ;
      textcolor(white+blink) ;
      write(capacity) ;
      textcolor(white) ;
      if ( mes = 'K' ) then
        write('     Kbps ')
      else
        write('     Bps') ;
    end ;
   if ( find = 'B' ) then
    begin
      temp :=  1 + (signal/noise) ;
      bandwidth := capacity / ( ln(temp) / ln(2) ) ;
      if ( mes = 'K' ) then
        bandwidth := bandwidth / 1000 ;
      gotoxy(10,20) ;
      write('The bandwidth is  :  ') ;
      textcolor(white+blink) ;
      write(bandwidth) ;
      textcolor(white) ;
      if ( mes = 'K' ) then
        write('     Khertz')
      else
        write('     Hertz') ;
    end ;
   sound(1500) ;
   delay(100) ;
   nosound ;
   sound(1300) ;
   delay(100) ;
   nosound ;
   sound(1000) ;
   delay(100) ;
   nosound ;
   readln ;
 end.
